package com.csnf.dao.impl;

import com.csnf.dao.IClaDao;
import com.csnf.entity.Cla;
import com.csnf.entity.Students;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class ClaDaoImpl implements IClaDao {
    @Override
    public List<Cla> findAll() throws IOException {
        SqlSession sqlSession = getSqlSession();
        List<Cla> claList = sqlSession.selectList("com.csnf.dao.IClaDao.findAll");
        sqlSession.close();
        return claList;
    }

    @Override
    public Cla findById(Integer id) throws IOException {
        SqlSession sqlSession = getSqlSession();
        Cla cla = sqlSession.selectOne("com.csnf.dao.IClaDao.findById",id);
        sqlSession.close();
        return cla;
    }

    private static SqlSession getSqlSession() throws IOException {
        String path = "mybatis-config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(path);
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(resourceAsStream);
        SqlSession sqlSession=sqlSessionFactory.openSession();
        return sqlSession;
    }
}
